﻿using System.Collections.Generic;
using AutoMapper;
using Navigant.DataContracts;
using Navigant.Infrastructure;
using Navigant.Infrastructure.Cases;
using Navigant.Mapping;
using Navigant.Model.Custodians;
using Navigant.ServiceInterfaces;

namespace Navigant.WebServices
{
    public class CustodianService : ICustodianService
    {
        #region ICustodianService Members

        public List<CustodianContract> GetCustodians(CaseContract parentCase)
        {
            List<CustodianContract> contracts = new List<CustodianContract>();
            ICase currentCase = Converter.ToCase(parentCase);
            Navigant.Model.Custodians.CustodianService service = new Navigant.Model.Custodians.CustodianService(currentCase, null, true);
            IList<Custodian> custodians = service.FindAll();
            foreach (Custodian custodian in custodians)
            {
                contracts.Add(Mapper.Map<Custodian, CustodianContract>(custodian));
            }
            return contracts;
        }

        public List<CustodianContract> GetCustodiansWithExtraCriteria(CaseContract parentCase, 
            string extraCriteriaFieldName, string extraCriteriaFieldValue)
        {
            List<CustodianContract> contracts = new List<CustodianContract>();
            ICase currentCase = Converter.ToCase(parentCase);
            Navigant.Model.Custodians.CustodianService service = new Navigant.Model.Custodians.CustodianService(currentCase, null, true);
            IList<Custodian> custodians = service.GetCustodiansBy(extraCriteriaFieldName, extraCriteriaFieldValue, false);
            foreach (Custodian custodian in custodians)
            {
                contracts.Add(Mapper.Map<Custodian, CustodianContract>(custodian));
            }
            return contracts;
        }

        public List<string> GetExtraCriteriaPossibleValues(CaseContract parentCase, 
            string extraCriteriaFieldName)
        {
            ICase currentCase = Converter.ToCase(parentCase);
            Navigant.Model.Custodians.CustodianService service = new Navigant.Model.Custodians.CustodianService(currentCase, null, true);
            return new List<string>(service.GetExtraCriteriaPossibleValues(extraCriteriaFieldName));
        }

        public void ImportCustodians(CaseContract parentCase, 
            List<CustodianContract> custodians)
        {
            ICase currentCase = Converter.ToCase(parentCase);
            IUnitOfWork unitOfWork = new UnitOfWork();
            Navigant.Model.Custodians.CustodianService service = new Navigant.Model.Custodians.CustodianService(currentCase, unitOfWork, true);
            List<Custodian> currentCustodians = new List<Custodian>(service.FindAll());

            foreach (CustodianContract contract in custodians)
            {
                if (!currentCustodians.Exists(delegate(Custodian custodian)
                {
                    return (contract.Email.Trim().ToLower() == custodian.Email.Trim().ToLower());
                }))
                {
                    contract.ParentCase = parentCase;
                    contract.Key = 0;
                    service.SaveCustodian(Mapper.Map<CustodianContract, Custodian>(contract));
                }
            }
            unitOfWork.Commit();
        }

        #endregion
    }
}
